import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {DynamicComponent, DynamicComponentEvent} from '../../../../../ng-tools';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';

@Component({
  selector: 'game-type-choose',
  templateUrl: './game-type-choose.component.html',
  styleUrls: ['./game-type-choose.component.scss']
})
export class GameTypeChooseComponent implements OnInit, DynamicComponent {
  context: any;
  dynamic: true;
  events: DynamicComponentEvent;
  currentGameGroup;
  set data(value) {
    this.currentGameGroup = value;
  }
  @Output() success = new EventEmitter();
  play_type;
  multi;
  times;
  level;
  is_join_special_reward;
  gameGroupForm: FormGroup;
  constructor(private formBuilder: FormBuilder) {
    this.gameGroupForm = this.formBuilder.group({
      play_type: ['', ],
      sendPacketUserOfJL: ['', ],
      multi: ['', ],
      times: ['', ],
      level: ['', ],
      low_account: ['', ],
      high_account: ['', ],
    });
  }
  ngOnInit() {
    if (this.currentGameGroup !== null) {
      this.chooseGameType(this.currentGameGroup.play_type);
      this.chooseNumber(this.currentGameGroup.multi);
      this.chooseTime(this.currentGameGroup.times);
      this.chooseMoney(this.currentGameGroup.level);
      this.chooseGift(this.currentGameGroup.is_join_special_reward);
    }
  }

  chooseGameType(type) {
    this.play_type = type;
  }
  chooseNumber(multiple) {
    this.multi = multiple;
  }
  chooseTime(interval) {
    this.times = interval;
  }
  chooseMoney(money) {
    this.level = money;
  }
  chooseGift(gift) {
    this.is_join_special_reward = gift;
  }
  trigger(eventName: string, ...args) {
    this[eventName].emit(...args);
    if (eventName in this.events) {
      this.events[eventName](...args);
    }
  }
  save() {
    this.gameGroupForm.patchValue({
      play_type: this.play_type,
      multi: this.multi,
      times: this.times,
      level: this.level,
      isJoinSpecialReward: this.is_join_special_reward,
    });
    this.trigger('success', this.gameGroupForm.value);
  }

}
